package yue.shen.android.demo.select

import android.graphics.Bitmap
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.BitmapImageViewTarget
import com.luck.picture.lib.entity.LocalMedia
import yue.shen.android.demo.R

class SelectImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val imageView = itemView.findViewById<ImageView>(R.id.image_view)
    private val notSelect = itemView.findViewById<ImageView>(R.id.image_select_n)
    private val select = itemView.findViewById<TextView>(R.id.tv_select)

    fun bindData(item: LocalMedia) {
        Glide.with(itemView.context)
            .asBitmap()
            .load(item.path)
            .apply(
                RequestOptions().placeholder(R.mipmap.select_picture_icon_placeholder).centerCrop()
                    .sizeMultiplier(0.5f).override(180, 180)
            )
            .into(object : BitmapImageViewTarget(imageView) {
                override fun setResource(resource: Bitmap?) {
                    val circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(itemView.context.resources, resource)
                    imageView.setImageDrawable(circularBitmapDrawable)
                }
            })
        if (item.num > 0) {
            notSelect.visibility = View.GONE
            select.visibility = View.VISIBLE
            select.text = "${item.num}"
        } else {
            notSelect.visibility = View.VISIBLE
            select.visibility = View.GONE
            select.text = ""
        }

    }

    fun getImageView(): ImageView = imageView

    fun getNotSelectView(): ImageView = notSelect

    fun getSelectView(): TextView = select

    fun setNotSelectClick(listener: View.OnClickListener) {
        notSelect.setOnClickListener(listener)
    }

    fun setSelectClick(listener: View.OnClickListener) {
        select.setOnClickListener(listener)
    }

}